package com.sbpm.crmbpm.client.grid;

import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.text.shared.AbstractSafeHtmlRenderer;
import com.sbpm.crmbpm.client.application.dict.Dictionaries;
import com.sbpm.crmbpm.client.forms.controls.CountryComboBox;
import com.sbpm.crmbpm.shared.data.client.Address;
import com.sbpm.crmbpm.shared.data.client.AddressType;
import com.sbpm.crmbpm.shared.data.client.Company;
import com.sbpm.crmbpm.shared.data.dict.City;
import com.sbpm.crmbpm.shared.data.dict.Country;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.editing.GridEditing;
import com.sencha.gxt.widget.core.client.grid.editing.GridRowEditing;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AddressGrid extends Grid<Address>
{

    private GridRowEditing<Address> gridEditing;

    public AddressGrid()
    {
        super( new ListStore< Address >( AddressColumnModelFactory.PROPS.key() ), AddressColumnModelFactory.createAddressColumnModel() );
        getView().setAutoExpandColumn(AddressColumnModelFactory.getAddressCol());
        getView().setForceFit( true );

        /**
         * todo remove it
         */



        List<Address> addressList = new ArrayList<>(  );

        Country country = Dictionaries.COUNTRIES.get( "Россия" );
        addressList.add( createAddress( 1l, "Это длинный адрес Это длинный адрес Это длинный адрес Это длинный адрес ", AddressType.MAIN,
                Dictionaries.CITIES.get( country ).get( "Москва" ) ) );
        addressList.add( createAddress( 2l, "улица небольшая ", AddressType.LEGAL,
                Dictionaries.CITIES.get( country ).get( "Новосибирск" ) ) );
        addressList.add( createAddress( 3l, "Это длинный адрес Это длинный адрес Это длинный адрес Это длинный адрес ", AddressType.POST,
                Dictionaries.CITIES.get( country ).get( "Новосибирск" ) ) );
        getStore().addAll( addressList );

        getView().refresh( false );

        gridEditing = new GridRowEditing<Address>( this );
        gridEditing.addEditor( AddressColumnModelFactory.getAddressCol(), new TextArea() );

        CountryComboBox combo = new CountryComboBox();
        // combo.setForceSelection(true);
        gridEditing.addEditor(AddressColumnModelFactory.getCountryCol(), combo);


        /*DateField dateField = new DateField(new DateTimePropertyEditor(
                DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));
        dateField.setClearValueOnParseError(false);
        editing.addEditor(cc3, dateField);

        CheckBox checkField = new CheckBox();
        editing.addEditor(cc4, checkField);*/

        // column 5 is not editable

        // EDITING//

        //customizeGrid(grid);


    }

    public GridRowEditing< Address > getGridEditing()
    {
        return gridEditing;
    }

    public Address createAddress(  Long id, String addressName, AddressType type, City city )
    {
        Address address = new Address();
        address.setId( id );
        address.setAddress( addressName );
        address.setType( type );
        address.setCity( city );
        return address;
    }


}
